/**
 * Created by xiewei on 15/八月/13.
 */
//截取url后缀
var topicId = '';
var stop = null;
var imgDir = 'img/';
var footer = $('footer');
//获取邮编
getRegionCode();
var getRegionTimer = setInterval(function(){
    if(root){
        clearInterval(getRegionTimer);
    }else{
        getRegionCode();
    }
},50);

/**
 * 获取话题列表
 * @type {number}
 * @type {string}
 */
var page = 1;
var getTopicList = function(page,topicId){
    if(page > 1){
        loading();
    }
    var topicListReqData = {
        dairyFor: '4',
        topicId: topicId+'',
        page: page+'',
        pageSize: '30'
    }
    $.ajax({
        url:' /dog2/appSocial/getDairyList',
        data: paramCoding(topicListReqData),
        type: 'POST',
        dataType: 'json',
        success: function(res){
            if(res.results){
                var results = res.results;
                for(var j = 0; j < results.length; j++){
                    var result = results[j];
                    var section = $('<section>').addClass('topic_diary');
                    //用户信息
                    var usrDiv = $('<div>').addClass('container');
                    if(result.face){
                        $('<img>').attr('src',result.face).attr('alt',result.uname).appendTo(usrDiv);
                    }else{
                        $('<img>').attr('src',imgDir+iconImgs.master).attr('alt',result.uname).appendTo(usrDiv);
                    }
                    var usrInfoDiv = $('<div>').addClass('usr_info');
                    $('<h1>').text(result.uname).appendTo(usrInfoDiv);
                    //用户地址信息
                    var address = $('<address>').appendTo(usrInfoDiv);
                    fillAddrInfo(result.uregionid,address);
                    usrInfoDiv.appendTo(usrDiv);

                    //日志内容
                    var photoDiv = $('<div>').addClass('pic_list');
                    if(result.mediaList.length > 0){
                        var medias = result.mediaList;
                        for(var i = 0; i < medias.length ; i ++){
                            var media = medias[i];
                            var divW = getPageWidth();
                            var divH = divW*media.coverScale;
                            var div = $('<div>').addClass('video');
                            var imgW = divW*0.2;
                            var img = $('<img>').attr('src',imgDir+'button_album_video_play.png')
                                .css({
                                    width: '20%',
                                    marginLeft: '-60%',
                                    marginTop: divH/2-imgW/2
                                });
                            var video = $('<video>').attr('src',media.media)
                                .attr('poster',media.cover+imgSize.thumbnail).attr('data',media.id)
                                .attr('preload','auto').addClass('stop')
                                .css({height: divH});
                            var p = $('<p>').text('您的浏览器不支持播放该视频:(').appendTo(video);
                            video.appendTo(div);
                            img.appendTo(div);
                            //点击播放\暂停
                            div.on('click',function(){
                                if(video.hasClass('stop')){
                                    video[0].play();
                                    video.removeClass('stop');
                                    video.next().addClass('hidden');

                                    var videoTimer = setInterval(function(){
                                        if(video[0].paused || video[0].ended){
                                            video.next().removeClass('hidden');
                                            clearInterval(videoTimer);
                                        }
                                    },1000);
                                }else if(!ifMobile()){
                                    video[0].pause();
                                    video.addClass('stop');
                                    video.next().removeClass('hidden');
                                }
                            });
                            video.on('click',function(e){
                                $(this)[0].play();
                                e.stopPropagation();
                            });
                            img.on('click',function(e){
                                $(this).prev()[0].play();
                                e.stopPropagation();
                            });
                            div.appendTo(photoDiv);
                        }
                    }
                    if(result.photos.length > 0){
                        var photos = result.photos;
                        $('<img>').addClass('cur_photo').attr('src',photos[0].original+imgSize.small).appendTo(photoDiv);
                        if(photos.length > 1){
                            var ul = $('<ul>').addClass('topic_pic_list');
                            for(var i = 0; i < photos.length ; i ++){
                                var photo = photos[i];
                                var span = $('<span>').attr('src',photo.original+imgSize.small)
                                    .css({//先显示默认图片
                                        background: 'url('+photo.original+imgSize.face+') no-repeat center center',
                                        backgroundSize: 'cover'
                                    });
                                if(i==0){span.addClass('selected')}
                                $('<li>').append(span).appendTo(ul);
                            }
                            var scroller = $('<div>').attr('id','scroller_'+j).addClass('scroller')
                                .css({ width: 90*photos.length });//所有图片总长 图片数*单张图片所占宽度
                            var wrapper = $('<div>').attr('id','wrapper_'+j).addClass('wrapper');
                            wrapper.append(scroller.append(ul)).appendTo(photoDiv);
                        }
                    }
                    //发布地址 时间
                    var bottomDiv = $('<div>').addClass('container');
                    var emoji = $('<pre>').addClass('content emoji_p').text(result.content).appendTo(bottomDiv);
                    $('<small>').text(result.address).appendTo(bottomDiv);
                    $('<time>').text(timeSettled(result.dateTime)).appendTo(bottomDiv);

                    section.append(usrDiv,photoDiv,bottomDiv).insertBefore(footer);
                    //多张图片
                    if(result.photos.length > 1){
                        bottomDiv.addClass('mt80');
                        //图片列表水平滚动
                        new IScroll('#wrapper_'+j, { scrollX: true, scrollY: false, mouseWheel: true, click: true });
                    }
                    //表情转换
                    emojifyTo(emoji);
                }
            }
            if(page > 1){
                loaded();
            }
            //图片点击事件 查看大图
            $(".topic_pic_list").each(function(){
                var curPhoto = $(this).parent().parent().prev();
                var picList = $(this);
                $(this).find('span').on('click',function(){
                    curPhoto.attr('src',$(this).attr('src'));
                    picList.find('.selected').removeClass('selected');
                    $(this).addClass('selected');
                })
            });
        }
    })
}
loading();
topicId = getUrlParam('topicId');
//获取用户信息
var topicReqData = {
    topicId: topicId
}
$.ajax({
    url:'/dog2/appSocial/getTopicInfo2',
    data: paramCoding(topicReqData),
    type: 'GET',
    dataType: 'json',
    success: function(res){
        //话题信息
        if(res.back){
            $('#shareBack').attr('src',res.back+imgSize.tiny).attr('alt',res.title);
        }
        if(res.face){
            $('#shareFace').attr('src',res.face+imgSize.face).attr('alt',res.title);
        }
        $('#shareTitle').text(res.title);
        $('head title').text('小狗在家-'+res.title);
        $('#shareHits').text(res.hits);
        $('#shareJoinCount').text(res.joinCount);
        var shareContent = $('#shareContent');
        shareContent.text(res.content);
        //话题列表
        getTopicList(page,res.id);
        loaded();
        //表情转换
        emojifyTo(shareContent);
    }
})
